﻿@using SubstrateHelper.Models;
@using SubstrateHelper.Data;
@using ProcessBase.Models;
@using System.Collections.Concurrent;
@using System.Collections.Generic;
@using System.Linq;
@using System.Timers;
@inject SubstrateHelperDbContext Db
@inject MessageService MessageService


<div>
    <div class="m-1">
        <span>
            <strong class="text-success">主批号 </strong>（包含）：
            <input type="text" @bind="LotNo" style="width:100px">
        </span>
        <span class="ml-5">
            <strong class="text-success">来料标识 </strong>（包含）：
            <input type="text" @bind="InputLotNo" style="width:100px">
        </span>
        <span class="ml-5">
            <strong class="text-success">产出标识 </strong>（包含）：
            <input type="text" @bind="OutputLotNo" style="width:100px">
        </span>

        <span class="ml-5">
            <strong class="text-success">工序 </strong>：
            <select @bind="SelectedProcess">
                <option value="-1">所有类型</option>
                @if (Processes != null)
                {
                    @for (int i = 0; i < Processes.Count; i++)
                    {
                        <option value="@i">@Processes[i]</option>
                    }
                }

            </select>
        </span>

       
    </div>

    <div class="m-1 mt-4">


        <span ><strong class="text-success">进站日期 </strong>从：<input type="date" @bind="InputDateStart"></span>
        <span class="ml-1">至：<input type="date" @bind="InputDateEnd"></span>

        <span class="ml-5">
            <strong class="text-success">出站状态 </strong> ：
            <select @bind="OutputState">
                <option value="所有">所有</option>
                <option value="未完成">未完成</option>
                <option value="已出站">已出站</option>
                <option value="未完成【逾期】">未完成【逾期】</option>
                <option value="已出站【逾期】">已出站【逾期】</option>
            </select>
        </span>

        <span class="ml-5">
            <strong class="text-success">质量状态 </strong> ：
            <select @bind="QualityState">
                <option value="所有">所有</option>
                <option value="未处理">未处理</option>
                <option value="流通">流通</option>
                <option value="正常流通">正常流通</option>
                <option value="让步流通">让步流通</option>
                <option value="扣留">扣留</option>
                <option value="报废">报废</option>
            </select>
        </span>




        <Button class="ml-5" IsOutline="false" Color="Color.Success" @onclick="DoQuery">查  询</Button>
    </div>


    <hr class="bg-primary" style="height:5px;" />
    <ComponentProcessStatistics Items="@Items" />


</div>
<Message />

@code {

    List<string> Processes
    {
        get
        {
            if (_Processes == null)
            {
                try
                {
                    _Processes = Db.Parameters.Select(x => x.ProcessName).Distinct().OrderBy(x => x).ToList();
                }
                catch
                {
                    _Processes = null;
                }
            }
            return _Processes;
        }


    }
    List<string> _Processes = null;

    int SelectedProcess { get; set; } = -1;


    string LotNo { get; set; }
    string OutputLotNo { get; set; }
    string InputLotNo { get; set; }



    DateTime? InputDateStart { get; set; }
    DateTime? InputDateEnd { get; set; }

    string OutputState { get; set; } = "所有";

    string QualityState { get; set; } = "所有";


    List<ProcessBase.Models.ProcessStatistics> Items { get; set; }





    private void DoQuery()
    {



        try
        {
            //批号（包括来料和产出）

            IQueryable<ProcessBase.Models.ProcessStatistics> query = Db.ProcessStatistics;

            //主批号
            if (!string.IsNullOrWhiteSpace(LotNo))
            {
                query = query.Where(x => x.MainLotNo.Contains(LotNo));

            }

            //来料标识
            if (!string.IsNullOrWhiteSpace(InputLotNo))
            {
                query = query.Where(x => x.LotNoInput.Contains(InputLotNo));

            }

            //产出标识
            if (!string.IsNullOrWhiteSpace(OutputLotNo))
            {
                query = query.Where(x => x.LotNoOutput.Contains(OutputLotNo));

            }


            //工序

            if (SelectedProcess >= 0)
            {
                query = query.Where(x => x.ProcessName == Processes[SelectedProcess]);
            }


            //进站日期
            if (InputDateStart != null)
            {
                query = query.Where(x => x.DateIn != null && x.DateIn.Value >= InputDateStart.Value);
            }
            if (InputDateEnd != null)
            {
                query = query.Where(x => x.DateIn != null && x.DateIn.Value <= InputDateEnd.Value);
            }




            //按出站状态:

            if (OutputState!="所有")
            {

                query = query.Where(x => x.ProcessState.Contains(OutputState));
            }




            //按质量状态

            if (QualityState != "所有")//所有
            {
                query = query.Where(x => x.QualityState.Contains(QualityState));
            }


            Items = query.OrderBy(x => x.DateIn).ToList();
            MessageService?.Show(new MessageOption()
            {
                Content = $"返回{Items.Count}条结果。",
                Icon = "fa fa-info-circle",
                ShowDismiss = true,
                Color = Color.Success
            });

        }
        catch (Exception ex)
        {
            MessageService?.Show(new MessageOption()
            {
                Content = $"发生错误：{ex.Message}",
                Icon = "fa fa-info-circle",
                ShowDismiss = true,
                Color = Color.Danger
            });
        }









    }






}
